package com.zrulin.ftcommunity.contorller.interceptor;

import com.zrulin.ftcommunity.pojo.User;
import com.zrulin.ftcommunity.service.DataStatisticsService;
import com.zrulin.ftcommunity.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zrulin
 * @create 2022-07-12 11:16
 */
@Component
public class DataStatisticsInterceptor implements HandlerInterceptor {

    @Autowired
    private DataStatisticsService dataStatisticsService;

    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //统计UV
        String ip = request.getRemoteHost();
        dataStatisticsService.recordUV(ip);

        //统计DAU
        User user = hostHolder.getUser();
        if(user != null){
            dataStatisticsService.recordDAU(user.getId());
        }
        return true;
    }
}
